package com.psp.bu.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.northend.util.NumUtil;
import com.psp.bu.controller.res.BaseResult;
import com.psp.bu.controller.res.ListResult;
import com.psp.bu.controller.res.bean.RCooperationBean;
import com.psp.bu.controller.springmvc.req.GetCooperationListParam;
import com.psp.bu.controller.springmvc.req.HandleCooperationParam;
import com.psp.bu.service.CooperationService;
import com.psp.bu.service.UserService;
import com.psp.bu.service.exception.ServiceException;
import com.psp.bu.service.impl.PageResult;
@Component
public class CooperationController {
	Logger logger = Logger.getLogger(CooperationController.class);

	
	@Autowired
	CooperationService cooperationServiceImpl;
	
	@Autowired
	UserService userServiceImpl;
	

	public ListResult<RCooperationBean> list(GetCooperationListParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RCooperationBean> result = new ListResult<>();
		String token = (String) request.getAttribute("token");
		try {
			String uid = (String) request.getAttribute("userId"); // 园区业务员id
			int page = NumUtil.toInt(param.getPage(), 0);
			int pageSize = NumUtil.toInt(param.getPageSize(), 10);
			String content = param.getContent();

			PageResult<RCooperationBean> resData = cooperationServiceImpl.getList(page, pageSize, uid, content);
			if (resData != null) {
				result.setTotalSize(resData.getCount());
				result.setData(resData.getData());
			}
			result.setPage(page);
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发送异常"));
		}
		result.setToken(token);
		return result;
	}


	public BaseResult handle(HandleCooperationParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		String cid = param.getCid();
		try {
			boolean flag = cooperationServiceImpl.handle(cid);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

}
